﻿using Minotaur.App.Core;
using Minotaur.App.Core.Contrls;
using Minotaur.App.Domains.Dto.Flow;
using Minotaur.App.Domains.Enum;
using Newtonsoft.Json;
using ServiceStack;
using SqlSugar.DistributedSystem.Snowflake;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Minotaur.App.Modules.FlowProcess.Control
{
    /// <summary>
    /// Interaction logic for NodeControl
    /// </summary>
    public partial class NodeControl : UserControl
    {
        public NodeControl()
        {
            InitializeComponent();
        }
        public readonly FlowStepInfoDto flowStepInfo;
        public NodeControl(FlowStepInfoDto _dataContextParam)
        {
            InitializeComponent();
            if (!string.IsNullOrEmpty(_dataContextParam.Coordinate))
            {
                double[] Coordinates = JsonConvert.DeserializeObject<double[]>(_dataContextParam.Coordinate);
                if (Coordinates != null && Coordinates.Any())
                {
                    _dataContextParam.ToLeft = Coordinates[0];
                    _dataContextParam.ToTop = Coordinates[1];
                    Canvas.SetLeft(this, _dataContextParam.ToLeft);
                    Canvas.SetTop(this, _dataContextParam.ToTop);
                }
            }
            this.flowStepInfo = _dataContextParam;
            this.DataContext = _dataContextParam;

        }
        /// <summary>
        /// 注册打开节点配置事件
        /// </summary>
        public static readonly RoutedEvent OpendNodeEvent =
              EventManager.RegisterRoutedEvent(ClickNameConst.OpendNodeClick, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(NodeControl));

        /// <summary>
        /// 注册节点删除事件
        /// </summary>
        public static readonly RoutedEvent DeleteNodeEvent =
             EventManager.RegisterRoutedEvent(ClickNameConst.DeleteNodeEventClick, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(NodeControl));

        /// <summary>
        /// 注册新增连线事件
        /// </summary>
        public static readonly RoutedEvent AddLineEvent =
            EventManager.RegisterRoutedEvent(ClickNameConst.AddLineEventClick, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(NodeControl));

        /// <summary>
        /// 注册新增连线事件
        /// </summary>
        public static readonly RoutedEvent NodeMoveDragEvent =
            EventManager.RegisterRoutedEvent(ClickNameConst.NodeMoveDropEventClick, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(NodeControl));

        /// <summary>
        /// 删除节点事件
        /// </summary>
        public event RoutedEventHandler DeleteNodeEventClick
        {
            add { AddHandler(DeleteNodeEvent, value); }
            remove { RemoveHandler(DeleteNodeEvent, value); }
        }

        /// <summary>
        /// 打开节点属性事件
        /// </summary>
        public event RoutedEventHandler OpendNodeClick
        {
            add { AddHandler(OpendNodeEvent, value); }
            remove { RemoveHandler(OpendNodeEvent, value); }
        }

        /// <summary>
        /// 新增连线事件
        /// </summary>
        public event RoutedEventHandler AddLineEventClick
        {
            add { AddHandler(AddLineEvent, value); }
            remove { RemoveHandler(AddLineEvent, value); }
        }

        /// <summary>
        /// 节点拖动事件
        /// </summary>
        public event RoutedEventHandler NodeMoveDropEventClick
        {
            add { AddHandler(NodeMoveDragEvent, value); }
            remove { RemoveHandler(NodeMoveDragEvent, value); }
        }

        /// <summary>
        /// 打开节点属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpendNodeProperty_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(OpendNodeEvent, this);
            RaiseEvent(args);
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteNode_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(DeleteNodeEvent, this);
            RaiseEvent(args);
        }

        private void node_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            ControlCommon.DragDelta(sender, e, this, NodeMoveDragEvent);
        }

        private void Node_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(OpendNodeEvent, this);
            RaiseEvent(args);
        }

        private void Button_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            base.OnMouseMove(e);
            ControlCommon.PreviewMouseMove(sender, e, this);
        }

        private void Button_Drop(object sender, DragEventArgs e)
        {
            base.OnDrop(e);
            //1、审批节点
            PropType[] propTypes = new PropType[] { PropType.ConditionsNode, PropType.BranchNode, PropType.EndBranchNode };
            if (this.flowStepInfo.Prop.Value != PropType.ApprovalNode)
            {
                propTypes = new PropType[] { PropType.ConditionsNode, PropType.ApprovalNode };
            }
            ControlCommon.Drop(sender,e,this,AddLineEvent,propTypes,this.flowStepInfo);
            e.Handled = true;
        }
    }
}
